package yana.tpe.servlets.task4.domain.dao.impl;

import java.io.Serializable;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import yana.tpe.servlets.task4.domain.dao.AbstractDao;
import yana.tpe.servlets.task4.domain.domains.AbstractEntity;


public abstract class AbstracDaoImpl
        <Key extends Serializable, Value extends AbstractEntity<Key>>
        implements AbstractDao <Key, Value> {

    @Autowired
    protected HibernateTemplate hibernateTemplate;

//    public void setSessionFactory(SessionFactory sf) {
//    	hibernateTemplate = new HibernateTemplate(sf);
//    }

    @Override
    public Value get(Key id) {
        return hibernateTemplate.get(getDomainClass(), id);
    }

    @Override
    public List<Value> list() {
        return hibernateTemplate.loadAll(getDomainClass());
    }

    @Override
    public Key save(Value object) {
        return (Key) hibernateTemplate.save(object);
    }

    @Override
    public void saveOrUpdate(Value object) {
        hibernateTemplate.saveOrUpdate(object);
    }

    @Override
    public void delete(Value object) {
        hibernateTemplate.delete(object);
    }

    protected abstract Class<Value> getDomainClass();

}
